//
//  LOGoodsViewController.m
//  dmao-customer
//
//  Created by macbook on 2017/10/19.
//  Copyright © 2017年 秦慕乔. All rights reserved.
//

#import "LOGoodsViewController.h"
#import "LOGoodsListModel.h"
#import "LOGoodsTableViewCell.h"
#import "LOGoodsDetailViewController.h"

@interface LOGoodsViewController ()<UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource>
{
    BOOL profit;
    BOOL agree;
    UITableView *_tab;
    UIView *_footerView;
    NSString *_ApiUrl;
}
@property (weak, nonatomic) IBOutlet UIView *NoStoreView;
@property (weak, nonatomic) IBOutlet UIView *StoreTopView;
@property (strong, nonatomic) UISearchBar *searchBar;
@property (strong, nonatomic) NSDictionary *dic;
@property (strong, nonatomic) LOGoodsListModel *goodsModel;
@property (weak, nonatomic) IBOutlet UIImageView *profitImg;
@property (weak, nonatomic) IBOutlet UIImageView *ArrowImg;
@property (weak, nonatomic) IBOutlet UIButton *SaleBtn;
@property (weak, nonatomic) IBOutlet UIButton *ProfitBtn;
@property (weak, nonatomic) IBOutlet UIButton *PriceBtn;
@property (weak, nonatomic) IBOutlet UIButton *StoreBtn;
@property (weak, nonatomic) IBOutlet UIButton *NewBtn;
@property (copy, nonatomic) NSString *priceStatus;
@property (copy, nonatomic) NSString *profitStatus;
@property (assign, nonatomic) NSInteger pageNumber;
@property (copy, nonatomic) NSString *type;
@property (copy, nonatomic) NSString *profitDes;
@property (copy, nonatomic) NSString *priceDes;
@end

@implementation LOGoodsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    if ([self.searchType isEqualToString:@"search"]) {
        self.titleLabel.text = self.searchGoodsName;
    }else if ([self.comeFrom isEqualToString:@"profit"]){
        self.titleLabel.text = @"分润点";
        _profitImg.hidden = YES;
        _ProfitBtn.enabled = NO;
    }else{
        self.titleLabel.text = self.goodsName;
    }
    
    if ([self.comeFrom isEqualToString:@"profit"]) {
        _ApiUrl = API(LOAllGoodsList);
        _type = @"2";
        [_ProfitBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [_SaleBtn setTitleColor:[UIColor colorWithHex:0x333333] forState:UIControlStateNormal];
        [_PriceBtn setTitleColor:[UIColor colorWithHex:0x333333] forState:UIControlStateNormal];
        [_StoreBtn setTitleColor:[UIColor colorWithHex:0x333333] forState:UIControlStateNormal];
        [_NewBtn setTitleColor:[UIColor colorWithHex:0x333333] forState:UIControlStateNormal];
    }else{
        _ApiUrl = API(LOGoodsList);
        _type = @"1";
        [_SaleBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [_ProfitBtn setTitleColor:[UIColor colorWithHex:0x333333] forState:UIControlStateNormal];
        [_PriceBtn setTitleColor:[UIColor colorWithHex:0x333333] forState:UIControlStateNormal];
        [_StoreBtn setTitleColor:[UIColor colorWithHex:0x333333] forState:UIControlStateNormal];
        [_NewBtn setTitleColor:[UIColor colorWithHex:0x333333] forState:UIControlStateNormal];
    }
    
    _goodsModel = [[LOGoodsListModel alloc] init];
    _pageNumber = 1;
    _StoreTopView.hidden = YES;
    [self downloadData];
    [self createSearchBar];
    [self createFooterView];
    [self createTableView];
}

- (void)createSearchBar
{
    _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(10, 80, WIDTH-20, 30)];
    _searchBar.backgroundColor = [UIColor clearColor];
    _searchBar.showsCancelButton = NO;
    _searchBar.placeholder = @"";
    _searchBar.delegate = self;
    for (UIView *subView in _searchBar.subviews) {
        if ([subView isKindOfClass:[UIView  class]]) {
            [[subView.subviews objectAtIndex:0] removeFromSuperview];
            if ([[subView.subviews objectAtIndex:0] isKindOfClass:[UITextField class]]) {
                UITextField *textField = [subView.subviews objectAtIndex:0];
                textField.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
                
                //设置默认文字颜色
                UIColor *color = [UIColor grayColor];
                [textField setAttributedPlaceholder:[[NSAttributedString alloc] initWithString:@"搜索商品/店铺" attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14.0f], NSForegroundColorAttributeName:color}]];
                //修改默认的放大镜图片
                UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 13, 13)];
                imageView.backgroundColor = [UIColor clearColor];
                imageView.image = [UIImage imageNamed:@"LineDownSearch"];
                textField.leftView = imageView;
            }
        }
    }
    [self.view addSubview:_searchBar];
}

#pragma mark-seacherDelegate
//回车键或者键盘的return触发
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    _pageNumber = 1;
    [_goodsModel.data removeAllObjects];
    if ([self.comeFrom isEqualToString:@"profit"]) {
        _dic = @{@"msort":_type, @"page":[NSNumber numberWithInteger:_pageNumber], @"page_size":@"20", @"mdesc":@"0", @"goodsName":_searchBar.text};
    }else{
        _dic = @{@"catId3":self.goodsId, @"msort":_type, @"page":[NSNumber numberWithInteger:_pageNumber], @"page_size":@"20", @"mdesc":@"0", @"goodsName":_searchBar.text};
    }

    [THWManager PostHttp:_ApiUrl Parameters:_dic Success:^(id responseObject) {
        [self.view removeAnyView];
        NSError *error;
        if ([responseObject[@"data"] isKindOfClass:[NSArray class]]) {
            for (NSDictionary *dic in [responseObject objectForKey:@"data"]) {
                GoodsListModel *model = [[GoodsListModel alloc] initWithDictionary:dic error:&error];
                [_goodsModel.data addObject:model];
            }
        }else{
            [self.view Message:responseObject[@"msg"]];
        }
        [_tab reloadData];
    } andFalse:^(NSError *error) {
        [self.view removeAnyView];
    }];
}

- (void)downloadData
{
    [self.view loadingOnAnyView];
    if ([self.searchType isEqualToString:@"search"]) {
        if ([_profitDes isEqualToString:@"profitDes"]) {
            _dic = @{@"msort":_type, @"mdesc":[NSString stringWithFormat:@"%d", profit], @"page":[NSNumber numberWithInteger:_pageNumber], @"page_size":@"20", @"goodsName":self.searchGoodsName};
        }else if ([_priceDes isEqualToString:@"priceDes"]) {
            _dic = @{@"msort":_type, @"mdesc":[NSString stringWithFormat:@"%d", agree], @"page":[NSNumber numberWithInteger:_pageNumber], @"page_size":@"20", @"goodsName":self.searchGoodsName};
        }else{
            _dic = @{@"msort":_type, @"page":[NSNumber numberWithInteger:_pageNumber], @"page_size":@"20", @"mdesc":@"0", @"goodsName":self.searchGoodsName};
        }
    }else if ([self.comeFrom isEqualToString:@"profit"]){
        if ([_profitDes isEqualToString:@"profitDes"]) {
            _dic = @{@"msort":_type, @"mdesc":[NSString stringWithFormat:@"%d", profit], @"page":[NSNumber numberWithInteger:_pageNumber], @"page_size":@"20"};
        }else if ([_priceDes isEqualToString:@"priceDes"]) {
            _dic = @{@"msort":_type, @"mdesc":[NSString stringWithFormat:@"%d", agree], @"page":[NSNumber numberWithInteger:_pageNumber], @"page_size":@"20"};
        }else{
            _dic = @{@"msort":_type, @"page":[NSNumber numberWithInteger:_pageNumber], @"page_size":@"20", @"mdesc":@"1"};
        }
    }else{
        if ([_profitDes isEqualToString:@"profitDes"]) {
            _dic = @{@"catId3":self.goodsId, @"msort":_type, @"mdesc":[NSString stringWithFormat:@"%d", profit], @"page":[NSNumber numberWithInteger:_pageNumber], @"page_size":@"20"};
        }else if ([_priceDes isEqualToString:@"priceDes"]) {
            _dic = @{@"catId3":self.goodsId, @"msort":_type, @"mdesc":[NSString stringWithFormat:@"%d", agree], @"page":[NSNumber numberWithInteger:_pageNumber], @"page_size":@"20"};
        }else{
            _dic = @{@"catId3":self.goodsId, @"msort":_type, @"page":[NSNumber numberWithInteger:_pageNumber], @"page_size":@"20", @"mdesc":@"0"};
        }
    }
    
    
    [THWManager PostHttp:_ApiUrl Parameters:_dic Success:^(id responseObject) {
        [self.view removeAnyView];
        NSError *error;
        if ([responseObject[@"data"] isKindOfClass:[NSArray class]]) {
            for (NSDictionary *dic in [responseObject objectForKey:@"data"]) {
                GoodsListModel *model = [[GoodsListModel alloc] initWithDictionary:dic error:&error];
                [_goodsModel.data addObject:model];
            }
        }else{
            [self.view Message:responseObject[@"msg"]];
        }
        [_tab reloadData];
        [_tab.mj_header endRefreshing];
        [_tab.mj_footer endRefreshing];
    } andFalse:^(NSError *error) {
        [self.view removeAnyView];
    }];
}

- (IBAction)SaleClick:(UIButton *)sender {
    [_SaleBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_ProfitBtn setTitleColor:[UIColor colorWithHex:0x333333] forState:UIControlStateNormal];
    [_PriceBtn setTitleColor:[UIColor colorWithHex:0x333333] forState:UIControlStateNormal];
    [_StoreBtn setTitleColor:[UIColor colorWithHex:0x333333] forState:UIControlStateNormal];
    [_NewBtn setTitleColor:[UIColor colorWithHex:0x333333] forState:UIControlStateNormal];
    _pageNumber = 1;
    [_goodsModel.data removeAllObjects];
    _type = @"1";
    [self downloadData];
}

- (IBAction)ProfitClick:(UIButton *)sender {
    [_ProfitBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_SaleBtn setTitleColor:[UIColor colorWithHex:0x333333] forState:UIControlStateNormal];
    [_PriceBtn setTitleColor:[UIColor colorWithHex:0x333333] forState:UIControlStateNormal];
    [_StoreBtn setTitleColor:[UIColor colorWithHex:0x333333] forState:UIControlStateNormal];
    [_NewBtn setTitleColor:[UIColor colorWithHex:0x333333] forState:UIControlStateNormal];
    _type = @"2";
    _pageNumber = 1;
    _profitDes = @"profitDes";
    _priceDes = @"NoPrice";
    if (!profit) {
        [_profitImg setImage:[UIImage imageNamed:@"LOGoodsDown"]];
        [self downloadData];
    }else {
        [_profitImg setImage:[UIImage imageNamed:@"LOGoodsTop"]];
        [self downloadData];
    }
    profit = !profit;
    
    [_goodsModel.data removeAllObjects];
}

- (IBAction)PriceClick:(UIButton *)sender {
    [_PriceBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_SaleBtn setTitleColor:[UIColor colorWithHex:0x333333] forState:UIControlStateNormal];
    [_ProfitBtn setTitleColor:[UIColor colorWithHex:0x333333] forState:UIControlStateNormal];
    [_StoreBtn setTitleColor:[UIColor colorWithHex:0x333333] forState:UIControlStateNormal];
    [_NewBtn setTitleColor:[UIColor colorWithHex:0x333333] forState:UIControlStateNormal];
    _type = @"3";
    _pageNumber = 1;
    _priceDes = @"priceDes";
    _profitDes = @"NoProfit";
    if (!agree) {
        [_ArrowImg setImage:[UIImage imageNamed:@"LOGoodsDown"]];
        [self downloadData];
    }else {
        [_ArrowImg setImage:[UIImage imageNamed:@"LOGoodsTop"]];
        [self downloadData];
    }
    agree = !agree;
    [_goodsModel.data removeAllObjects];
}

- (IBAction)StoreClick:(UIButton *)sender {
    [_StoreBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_SaleBtn setTitleColor:[UIColor colorWithHex:0x333333] forState:UIControlStateNormal];
    [_PriceBtn setTitleColor:[UIColor colorWithHex:0x333333] forState:UIControlStateNormal];
    [_ProfitBtn setTitleColor:[UIColor colorWithHex:0x333333] forState:UIControlStateNormal];
    [_NewBtn setTitleColor:[UIColor colorWithHex:0x333333] forState:UIControlStateNormal];
    _pageNumber = 1;
}

- (IBAction)NewClick:(UIButton *)sender {
    [_NewBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_SaleBtn setTitleColor:[UIColor colorWithHex:0x333333] forState:UIControlStateNormal];
    [_PriceBtn setTitleColor:[UIColor colorWithHex:0x333333] forState:UIControlStateNormal];
    [_StoreBtn setTitleColor:[UIColor colorWithHex:0x333333] forState:UIControlStateNormal];
    [_ProfitBtn setTitleColor:[UIColor colorWithHex:0x333333] forState:UIControlStateNormal];
    _pageNumber = 1;
    [_goodsModel.data removeAllObjects];
    _type = @"4";
    [self downloadData];
}

- (void)createFooterView
{
    _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 1)];
    _footerView.backgroundColor = RGB(232, 234, 234);
}

- (void)createTableView
{
    _tab = [[UITableView alloc] initWithFrame:CGRectMake(0, 158, WIDTH, HEIGHT-158)];
    _tab.delegate = self;
    _tab.dataSource = self;
    [_tab setTableFooterView:_footerView];
    [_tab registerNib:[UINib nibWithNibName:@"LOGoodsTableViewCell" bundle:nil] forCellReuseIdentifier:@"LOGoodsTableViewCell"];
    _tab.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tab];
    
    _tab.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [_goodsModel.data removeAllObjects];
        _pageNumber = 1;
        [self downloadData];
    }];
    _tab.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _pageNumber ++;
        [self downloadData];
    }];
}

#pragma mark - UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _goodsModel.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LOGoodsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LOGoodsTableViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[LOGoodsTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"LOGoodsTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell cellConfigureModel:_goodsModel.data[indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LOGoodsDetailViewController *detail = [[LOGoodsDetailViewController alloc] init];
    detail.goodsId = [NSString stringWithFormat:@"%@", [_goodsModel.data[indexPath.row] goodsId]];
    [self.navigationController pushViewController:detail animated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [_searchBar resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
